package com.lizhanglong.androidtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lizhanglong.core.AsyncTaskHelper;
import com.lizhanglong.core.HttpHelper;

public class HttpTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_test);

        final TextView resultTV = (TextView) findViewById(R.id.tvResult);
        Button btnRequest = (Button) findViewById(R.id.btnRequest);
        final EditText txtUrl = (EditText) findViewById(R.id.txtUrl);

        final HttpHelper httpHelper = new HttpHelper();
        httpHelper.setTrustAllHttpsCertificates(true);
        final AsyncTaskHelper asyncTaskHelper = new AsyncTaskHelper(new AsyncTaskHelper.ITaskCallback() {

            @Override
            public Object Execute(Object[] params) {
                String result = null;
                try {
                    result = httpHelper.httpGet(params[0].toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    result = e.toString();
                }
                return result;
            }

            @Override
            public void Complete(Object result) {
                resultTV.setText(result.toString());
            }
        });
        btnRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                asyncTaskHelper.execute(txtUrl.getText());
            }
        });


    }
}
